﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MvcSmartTool.Common {
    public interface IGenerationAttribute {
        string ToStringForModel();
        string ToStringForClientModel();
    }

    public class MaxAttr : IGenerationAttribute {
        public MaxAttr(int length) {
            Length = length;
        }
        public MaxAttr(int length, string errorMess)
            : this(length) {
            ErrorMessage = errorMess;
        }
        public int Length { get; set; }
        public string ErrorMessage { get; set; }
        public string ToStringForModel() {
            return string.IsNullOrEmpty(ErrorMessage) ? string.Format("[MaxLength({0})]", Length) : string.Format("[MaxLength({0},\"{1}\")]", Length, ErrorMessage);
        }

        public string ToStringForClientModel() {
            return string.Format(".extend({{maxLength: {0}}})", Length);
        }
    }

    public class MinAttr : IGenerationAttribute {
        public MinAttr(int length) {
            Length = length;
        }
        public int Length { get; set; }
        public string ToStringForModel() {
            return string.Format("[MinLength({0})]", Length);
        }

        public string ToStringForClientModel() {
            return string.Format(".extend({{minLength: {0}}})", Length);
        }
    }

    public class DataTypeAttr : IGenerationAttribute {
        public DataTypeAttr(string type) {
            Type = type;
        }
        public string Type { get; set; }
        public string ToStringForModel() {
            return string.Format("[DataType(DataType.{0})]", Type);
        }

        public string ToStringForClientModel() {
            var type = Type.ToLower();
            if (type == "email" || type == "date") {
                return string.Format(".extend({{ {0}: true}})", type);
            }
            return string.Empty;
        }
    }

    public class RequiredAttr : IGenerationAttribute {
        public string ToStringForModel() {
            return "[Required]";
        }

        public string ToStringForClientModel() {
            return ".extend({required: true})";
        }
    }
}